package com.itany.boot.controller;

import com.itany.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/f1")
    public String f1(){
        return "hello";
    }

    @RequestMapping("/f2")
    public ModelAndView f2(HttpSession session){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","<h1>Hello Thymeleaf</h1>");

        User user = new User(1,"孙尚香","123456","13812345678","江苏-南京");
        User user2 = new User(3,"瑶","123456","13812345678","江苏-南京");
        User user3 = new User(6,"安其拉","123456","13812345678","江苏-南京");
        User user4 = new User(7,"花木兰","123456","13812345678","江苏-南京");
        User user5 = new User(9,"李白","123456","13812345678","江苏-南京");

        session.setAttribute("user",user);
        session.getServletContext().setAttribute("user2",user2);

        mav.addObject("id",12);
        mav.addObject("age",20);
        mav.addObject("role","programmer");

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);
        mav.addObject("users",users);
        mav.addObject("user",user);

        mav.setViewName("tf");
        return mav;
    }

}
